package com.time.admin.domain.converter;

import com.time.admin.common.config.BackupProperties;
import com.time.admin.common.config.EmailProperties;
import com.time.admin.common.config.WebsiteProperties;
import com.time.admin.domain.entity.SettingBO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

/**
 * 配置信息BO转换类
 *
 * @author: HuangQi
 * @date: Created in 14:18 2025/11/12
 * @description: 配置信息BO转换类
 */
@Mapper(componentModel = "spring")
public interface SettingBOConverter {
    // 注意：你也可以提供 INSTANCE 实例，但在 Spring 环境推荐注入方式
    SettingBOConverter INSTANCE = Mappers.getMapper(SettingBOConverter.class);

    // 映射 WebsiteSetting → WebsiteProperties
    @Mapping(target = "siteName", source = "website.siteName")
    @Mapping(target = "description", source = "website.description")
    @Mapping(target = "keywords", source = "website.keywords")
    @Mapping(target = "logoUrl", source = "website.logoUrl")
    @Mapping(target = "faviconUrl", source = "website.faviconUrl")
    @Mapping(target = "icp", source = "website.icp")
    @Mapping(target = "copyright", source = "website.copyright")
    @Mapping(target = "contactEmail", source = "website.contactEmail")
    void mapWebsite(SettingBO settingBO, @MappingTarget WebsiteProperties websiteProperties);

    // 映射 EmailSetting → EmailProperties（简例）
    @Mapping(target = "enabled", source = "email.enabled")
    @Mapping(target = "defaultFrom", source = "email.defaultFrom")
    @Mapping(target = "smtp.host", source = "email.host")
    @Mapping(target = "smtp.port", source = "email.port")
    @Mapping(target = "smtp.username", source = "email.username")
    @Mapping(target = "smtp.password", source = "email.password")
    @Mapping(target = "smtp.fromName", source = "email.fromName")
    @Mapping(target = "smtp.sslEnabled", source = "email.sslEnabled")
    void mapEmail(SettingBO settingBO, @MappingTarget EmailProperties emailProperties);

    // 映射 BackupSetting → BackupProperties
    default void mapBackup(SettingBO settingBO, @MappingTarget BackupProperties backupProperties) {
        if (settingBO.getBackup() != null) {
            if (settingBO.getBackup().getKeepCount() != null) {
                backupProperties.setKeepCount(settingBO.getBackup().getKeepCount());
            }
            if (settingBO.getBackup().getEnabled() != null) {
                backupProperties.setEnabled(settingBO.getBackup().getEnabled());
            }
            if (settingBO.getBackup().getContents() != null) {
                backupProperties.setContents(
                        settingBO.getBackup().getContents().stream()
                                .map(BackupProperties.Content::getByName)
                                .filter(java.util.Objects::nonNull)
                                .collect(java.util.stream.Collectors.toList())
                );
            }
        }
    }
}
